﻿using System;
using System.Reflection;
using Dsl.Ped.Framework.Members;

namespace Dsl.Ped.Framework.Helpers
{
    /// <summary>
    /// Helper methods for <see cref="MemberInfo"/>
    /// </summary>
    public class MemberInfoHelper
    {
        /// <summary>
        /// Converts <see cref="MemberInfo"/> into the <see cref="Member"/>
        /// </summary>
        /// <param name="memberInfo"><see cref="MemberInfo"/> to be converted</param>
        /// <returns><see cref="Member"/> converted from the given <see cref="memberInfo"/></returns>
        public static Member ConvertToMember(MemberInfo memberInfo)
        {
            if (memberInfo == null)
            {
                throw new NullReferenceException("MemberInfo not specified");
            } 

            if (memberInfo is PropertyInfo)
            {
                return ConvertToMember(memberInfo as PropertyInfo);
            }

            throw new InvalidOperationException("Unable to convert the given memberInfo to Member");
        }

        /// <summary>
        /// Converts <see cref="PropertyInfo"/> into the <see cref="Member"/>
        /// </summary>
        /// <param name="propertyInfo"><see cref="PropertyInfo"/> to be converted</param>
        /// <returns><see cref="Member"/> converted from the given <see cref="propertyInfo"/></returns>
        public static Member ConvertToMember(PropertyInfo propertyInfo)
        {
            if (propertyInfo == null)
            {
                throw new NullReferenceException("PropertyInfo not specified");
            }
            return new PropertyMember(propertyInfo);
        }
    }
}
